import chess
from random import *

cols = ['a','b','c','d','e','f','g','h']
rows = ['1','2','3','4','5','6','7','8']

class dave:
	def __init__(self,colour):
		self.b = None
		self.colour = colour
	def move(self,game):
		self.b = game.board.clone()
		moves = self.possiblemoves()
		print moves
		pick = moves[randint(0,len(moves)-1)]
		while len(pick[1]) == 0:
			pick = moves[randint(0,len(moves)-1)]
		actualmove = pick[0] + "-" + pick[1][randint(0,len(pick[1])-1)]
		print actualmove
		move = game.move(actualmove)
		game.advance()
		
	def possiblemoves(self):
		
		possiblemoves = []
		for i in range(0,8):
			for j in range(0,8):
				square = cols[j] + rows[i]
				if self.colourat(square) == self.colour:
					loc = chess.AlgebraicLoc(square)
					piece = self.b.get(loc)
					presymbol = str(piece.symbol[0]) + square
					possiblemoves.append([presymbol,self.testloc(square)])
					
		return possiblemoves
		
	def colourat(self,location):
		
		loc = chess.AlgebraicLoc(location)
		piece = self.b.get(loc)
		if piece != -1:
			return piece.color
		else:
			return -1	
	def testloc(self,square):
		loc = chess.AlgebraicLoc(square)
		piece = self.b.get(loc)
		if piece != -1:
			return piece.possible(self.b)
		else:
			return -1
